package com.android.mtchat;

import java.io.IOException;

import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.DownloadManager.Request;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivityLogin extends Activity {
	
	TextView tvCreateAC ;
	Button btlogin;
	EditText id,pass; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_login);
		 //thiet lap ket noi internet
		 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		 StrictMode.setThreadPolicy(policy);
		tvCreateAC = (TextView)findViewById(R.id.tvCreateAccount);
		btlogin = (Button)findViewById(R.id.btnLoggin);
		id = (EditText)findViewById(R.id.edUser_id);
		pass =(EditText)findViewById(R.id.edUser_pass);
		
		tvCreateAC.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				Intent i = new Intent(v.getContext(), MainActivityCreateAccount.class);
				startActivity(i);
				//finish();
				
			}
		});
		
		btlogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				// TODO Auto-generated method stub
				XmppManager xmpp=new XmppManager("115.73.103.233",5222);
				try {
					xmpp.init();
				} catch (XMPPException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				try {
					int kq=xmpp.performLogin(id.getText().toString(), pass.getText().toString());

					if(kq==1)
					{
						Toast.makeText(getApplicationContext(),"Thanh Cong", Toast.LENGTH_SHORT).show();
						Intent i = new Intent(getApplicationContext(), MainActivity.class);
						startActivity(i);
						finish();
					}
					else
					{
						Toast.makeText(getApplicationContext(), "Kết nối thất bại", Toast.LENGTH_SHORT).show();
					}
				
				} catch (XMPPException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			
				//finish();
				
			}
		});
	}

}
